// sub_zhonghang/module2/page_yxtz/page2/page2.js
const dayjs = require("dayjs");
import Toast from "@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    recordId: "",
    // 企业列表下拉框
    dropdownIsShow: false,
    prosearchlodding: false,
    proinputisfocus: false,
    prolist: [],

    form: {
      enterpriseName: "",
      enterpriseCode: "",
      clientCode: "",
      clientAddress: "",
      jointName: "",
      jointDuty: "",
      jointMatter: "陌生拜访",
      jointMatterIndex: "0",
      jointContent: "新客户",
      jointContentIndex: "0",
      authMoney: "",
      authBalance: "",
      drawingsTime: dayjs(new Date(new Date().getTime())).format("YYYY-MM-DD"),
      drawingsMoney: "",
      markName: "",
      markContent: "",
      markAccomp: "",
      ismarkAtm: "1",
      markAtmMoney: "",
      clockInVO: [],
      isTobacconist: "否",
    },
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      return value;
    },
    pf_date_in: new Date().getTime(),
    jointMatterArr: [
      "陌生拜访",
      "日常维护",
      "授信营销",
      "贷后维护",
      "其他事项",
    ],
    jointMatterIsshow: false,
    jointContentArr: ["新客户", "已批未提", "其他"],
    jointContentIsshow: false,
    drawingsTimeIshow: false,
  },
  // methods
  // 企业名称
  setproname(e) {
    console.log(e);
    this.setData({
      ["form.enterpriseName"]: e.detail.value,
    });
    this.getProlist();
  },
  getProlist() {
    var _this = this;
    // wx.showLoading({
    //   title: '搜索中',
    // })
    let prams = {
      corpName: this.data.form.enterpriseName,
    };
    console.log(prams);
    this.setData({
      prosearchlodding: true,
    });
    if (this.data.form.enterpriseName.length > 0) {
      getApp()
        .globalData.request("/boc/bocSales/getCorpNames", prams, "POST")
        .then((res) => {
          console.log("++++++++", res);
          // wx.hideLoading()
          if (res.data.length > 0) {
            _this.setData({
              prolist: res.data,
              prosearchlodding: false,
              dropdownIsShow: true,
            });
            this.opendropdown();
          } else {
            _this.setData({
              prolist: [],
              prosearchlodding: false,
            });
            this.closedropdown();
          }
        });
    } else {
      _this.setData({
        prolist: [],
      });
      this.closedropdown();
    }
  },
  // 企业名称获取焦点
  opendropdown() {
    if (this.data.form.enterpriseName.length > 0) {
      // this.setData({
      //   dropdownIsShow: true
      // })
    }
  },
  closedropdown() {
    this.setData({
      dropdownIsShow: false,
    });
  },
  proitemClick(e) {
    console.log(e.currentTarget.dataset.proname);
    this.setData({
      ["form.enterpriseName"]: e.currentTarget.dataset.proname,
      ["form.enterpriseCode"]: e.currentTarget.dataset.prokey,
    });
    this.setData({
      dropdownIsShow: false,
      // prolist: [],
      proinputisfocus: true,
    });
  },
  // 输入框
  enterpriseCodeInput(e) {
    this.setData({
      ["form.enterpriseCode"]: e.detail.value,
    });
  },
  clientCodeInput(e) {
    this.setData({
      ["form.clientCode"]: e.detail.value,
    });
  },
  clientAddressInput(e) {
    this.setData({
      ["form.clientAddress"]: e.detail.value,
    });
  },
  jointNameInput(e) {
    this.setData({
      ["form.jointName"]: e.detail.value,
    });
  },
  jointDutyInput(e) {
    this.setData({
      ["form.jointDuty"]: e.detail.value,
    });
  },
  markContentInput(e) {
    this.setData({
      ["form.markContent"]: e.detail.value,
    });
  },
  markAccompInput(e) {
    this.setData({
      ["form.markAccomp"]: e.detail.value,
    });
  },
  markNameInput(e) {
    this.setData({
      ["form.markName"]: e.detail.value,
    });
  },
  ismarkAtmonChange(e) {
    // console.log(e);
    this.setData({
      ["form.ismarkAtm"]: e.detail,
    });
  },
  isTobacconistChange(e) {
    // console.log(e);
    this.setData({
      ["form.isTobacconist"]: e.detail,
    });
  },
  markAtmMoneyInput(e) {
    this.setData({
      ["form.markAtmMoney"]: e.detail.value,
    });
  },
  authMoneyInput(e) {
    this.setData({
      ["form.authMoney"]: e.detail.value,
    });
  },
  authBalanceInput(e) {
    this.setData({
      ["form.authBalance"]: e.detail.value,
    });
  },
  drawingsMoneyInput(e) {
    this.setData({
      ["form.drawingsMoney"]: e.detail.value,
    });
  },
  // 对接实现选择器
  jointMatterClick() {
    this.setData({
      jointMatterIsshow: true,
    });
  },
  jointMatteCancel() {
    this.setData({
      jointMatterIsshow: false,
    });
  },
  jointMatteConfirm(e) {
    console.log(e);
    this.setData({
      ["form.jointMatter"]: e.detail.value,
      ["form.jointMatterIndex"]: e.detail.index,
      jointMatterIsshow: false,
    });
  },
  jointContentClick() {
    this.setData({
      jointContentIsshow: true,
    });
  },
  jointContentCancel() {
    this.setData({
      jointContentIsshow: false,
    });
  },
  jointContentConfirm(e) {
    this.setData({
      ["form.jointContent"]: e.detail.value,
      ["form.jointContentIndex"]: e.detail.index,
      jointContentIsshow: false,
    });
  },
  drawingsTimeClick() {
    this.setData({
      drawingsTimeIshow: true,
    });
  },
  drawingsTimeCancel() {
    this.setData({
      drawingsTimeIshow: false,
    });
  },
  drawingsTimeConfirm(e) {
    this.setData({
      ["form.drawingsTime"]: dayjs(new Date(e.detail)).format("YYYY-MM-DD"),
      drawingsTimeIshow: false,
    });
  },
  // 提交
  commitClick() {
    console.log(this.data.form.enterpriseCode);
    if (this.data.form.enterpriseName == "") {
      Toast("请先填写客户基础信息！");
    } else if (this.data.form.jointMatter == "") {
      Toast("请先选择对接事项！");
    } else if (this.data.form.markContent == "") {
      Toast("请先填写营销内容！");
    } else if (this.data.form.jointContent == "") {
      Toast("请先填写对接内容！");
    } else if (this.data.form.jointContent == "已批未提") {
      if (!this.data.form.authMoney || this.data.form.authMoney == "") {
        Toast("请先填写批复金额！");
      } else if (
        !this.data.form.authBalance ||
        this.data.form.authBalance == ""
      ) {
        Toast("请先填写授信余额！");
      } else if (
        !this.data.form.drawingsMoney ||
        this.data.form.drawingsMoney == ""
      ) {
        Toast("请先填写预计提款金额！");
      } else {
        if (this.data.form.ismarkAtm == "0") {
          if (
            !this.data.form.markAtmMoney ||
            this.data.form.markAtmMoney == ""
          ) {
            Toast("请先营销提款金额！");
          } else {
            this.commitRequest();
          }
        } else {
          this.commitRequest();
        }
      }
    } else {
      if (this.data.form.ismarkAtm == "0") {
        if (!this.data.form.markAtmMoney || this.data.form.markAtmMoney == "") {
          Toast("请先营销提款金额！");
        } else {
          this.commitRequest();
        }
      } else {
        this.commitRequest();
      }
    }
  },
  commitRequest() {
    console.log(this.data.form.enterpriseCode);
    let prams = {
      meetName: this.data.form.jointName, //对接人
      meetDuty: this.data.form.jointDuty, //职务
      meetItems: this.data.form.jointMatterIndex * 1 + 1, //对接事项
      salesDetails: this.data.form.markContent, //营销内容
      salesAchievemENT: this.data.form.markAccomp, //营销成果
      corpName: this.data.form.enterpriseName, //企业名称
      customerNo: this.data.form.clientCode, //客户号
      corpCredit: this.data.form.enterpriseCode, //统一信用代码
      area: this.data.form.clientAddress, //地址
      meetDetails: this.data.form.jointContentIndex * 1 + 1, //对接内容
      approvalMoney: this.data.form.authMoney, //批复金额
      creditMoney: this.data.form.authBalance, //授信余额
      planTime: this.data.form.drawingsTime, //计划提款时间
      planMoney: this.data.form.drawingsMoney, // 预计提款金额
      accompanyPerson: this.data.form.markName, // 陪同营销人员
      isMarketDrawMoney: this.data.form.ismarkAtm, // 是否成功营销提款
      isTobacconist: this.data.form.isTobacconist, // 是否为烟商客户
      marketDrawMoney: this.data.form.markAtmMoney, // 营销提款金额
      recordId: this.data.recordId,
    };
    console.log(prams);
    getApp()
      .globalData.request("/boc/bocSales/saveSalesRegister", prams, "POST")
      .then((res) => {
        console.log(res);
        if (res.code == 200) {
          Toast("提交成功！");
          let pages = getCurrentPages();
          let prevPage = pages[pages.length - 2];
          prevPage.setData({
            // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
            refresh: true,
          });
          wx.navigateBack({
            delta: 1, // 返回上一级页面。
          });
        }
      });
  },
  // 判断jointMatter
  jointMatterUtil(name) {
    ["", "", "", "", ""];
    switch (name) {
      case "陌生拜访":
        return "0";
      case "日常维护":
        return "1";
      case "授信营销":
        return "2";
      case "贷后维护":
        return "3";
      case "其他事项":
        return "4";
      default:
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    if (options.recordId) {
      this.setData({
        recordId: options.recordId,
      });
      getApp()
        .globalData.request(
          "/boc/bocSales/getSaleLedger",
          {
            recordId: options.recordId,
          },
          "POST"
        )
        .then((res) => {
          console.log(res);
          let obj = res.data;
          this.setData({
            ["form.jointName"]: obj.meetName,
            ["form.jointDuty"]: obj.meetDuty,
            ["form.jointMatter"]: obj.label.name,
            ["form.jointMatterIndex"]: this.jointMatterUtil(obj.label.name),
            ["form.markContent"]: obj.salesDetails,
            ["form.markAccomp"]: obj.salesAchievement,
            ["form.enterpriseName"]: obj.corpName,
            ["form.clientCode"]: obj.customerNo,
            ["form.enterpriseCode"]: obj.corpCredit,
            ["form.clientAddress"]: obj.area,
            //
            ["form.jointContent"]: obj.meetDetail,
            ["form.jointContentIndex"]:
              obj.meetDetail == "新客户"
                ? "0"
                : obj.meetDetail == "已批未提"
                ? "1"
                : "2",
            ["form.authMoney"]: obj.approvalMoney,
            ["form.authBalance"]: obj.creditMoney,
            ["form.drawingsTime"]: obj.planTime,
            ["form.drawingsMoney"]: obj.planMoney,
            ["form.clockInVO"]: obj.clockInVO,
            ["form.markName"]: obj.accompanyPerson,
            ["form.ismarkAtm"]: obj.isMarketDrawMoney == "是" ? "0" : "1",
            ["form.markAtmMoney"]: obj.marketDrawMoney,
            ["form.isTobacconist"]: obj.isTobacconist,
          });
          console.log(this.data.form);
        });
    } else {
      this.setData({
        recordId: "",
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
